<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Postbacks extends CI_Controller {

	public function __construct(){
		parent::__construct();

		// load helper
		$this->load->helper('url');

		/* load model */
		$this->load->model("Lead", '', TRUE);
		$this->load->model("Network", '', TRUE);
	}	

	public function blvd(){

		// Your Blvd-Media Password
		$bmgpass = $this->Network->getPassOrSecretKey(BLVD);
			
			// Authenticate Session via Blvd-Media Password
			if ($bmgpass == $_GET['Validate']) {

				// Username of the user that earned the reward(s).
				$username = mysql_real_escape_string($_GET['SubId']);
				// $username = $_GET['SubId'];

				// Blvd-Media's Offer Campaign ID
				$offerId = mysql_real_escape_string($_GET['CampId']);
				// $offerId = $_GET['CampId'];

				// Amount that the user has earned
				$point = mysql_real_escape_string($_GET['Earn']);
				// $point = $_GET['Earn'];

				// Section to Increment User Points/Rewards via MySQL
				// Enter your incrementation code here

				$this->Lead->add( $offerId, BLVD, $username, $point );

				// mysql_close(); // Closes MySql Connection
				echo "RewardTool&reg; Crediting Success: ".$username." earned ".$point." points.";
			}
			else {

				$this->Lead->add( "error", 1, "error", 9 );
				echo "Error: IP Not Accepted";
				exit;
			}       
	}


	public function superrewards(){


		$id = $_REQUEST['id']; // ID of this transaction.
		$uid = $_REQUEST['uid']; // ID of the user which performed this transaction. 
		$oid = $_REQUEST['oid']; // ID of the offer or direct payment method.
		$new = $_REQUEST['new']; // Number of in-game currency your user has earned by completing this offer.
		$total = $_REQUEST['total']; // Total number of in-game currency your user has earned on this App.
		$sig = $_REQUEST['sig']; // Security hash used to verify the authenticity of the postback.

		/**
		 * Sanity check.
		 *
		 * If you are using alphanumeric user ids, remove the is_numeric($uid) check. Alphanumeric
		 * ids can only be enabled by Super Rewards Support
		 *
		 * If you are using alphanumeric user ids, please ensure that you use the appropriate URL-encoding
		 * for non-text or unicode characters. For example: ~ should be encoded as %7E
		 */
			if(!(is_numeric($id) && is_numeric($oid) && is_numeric($new) && is_numeric($total))){

				echo 1;
				// exit('0'); // Fail.
			}

			else{

				$secretKey = $this->Network->getPassOrSecretKey(SUPEREWARDS);
				$sig_compare = md5($id.':'.$new.':'.$uid.':'.$secretKey);

				// Only accept if the Security Hash matches what we have.
				if($sig == $sig_compare){

					$this->Lead->add( $id, SUPEREWARDS, $uid, $new );
					echo 1;
				}
				else{
					echo 0; // Security hash incorrect. Fail.
				}
			}

	}


	public function cpagrip(){


		// $bmgpass = $this->Network->getPassOrSecretKey(CPAGRIP);
		// if($_POST['password'] != $bmgpass){
	 //    	echo "Access Denied.";
	 //    	exit;
		// }


		$this->Lead->add( $_POST['offer_id'], CPAGRIP, $_POST['tracking_id'], $_POST['payout'] * 100 * $this->Network->getBanner_ratio(CPAGRIP) );
	
	}

	public function offersrevenue(){

		// if($_SERVER['REMOTE_ADDR'] == "23.254.162.42"){

			$this->Lead->add( $_GET['campaignname'], OFFERSREVENUE, $_GET['yti'], $_GET['credit'] * $this->Network->getBanner_ratio(OFFERSREVENUE) );
		// }
		// else{
		// 	echo "you are cheater :( ";
		// }
	}

	public function ultrapay(){

		// if($_SERVER['REMOTE_ADDR'] == "162.243.76.74"){

			$this->Lead->add( $_GET['offer_id'], ULTRAPAY, $_GET['subid'], $_GET['comission'] * $this->Network->getBanner_ratio(OFFERSREVENUE) );
		// }
		// else{
		// 	echo "you are cheater :( ";
		// }		
	}

	public function betternetwork(){


			$this->Lead->add( $_GET['offer_name'], BETTERNETWORK, $_GET['aff_sub'], $_GET['payout'] * $this->Network->getBanner_ratio(BETTERNETWORK) );
	}	

	public function ksix(){


			$this->Lead->add( $_GET['offer_name'], KSIX, $_GET['aff_sub'], $_GET['payout'] * $this->Network->getBanner_ratio(KSIX) );
	}

	public function cpalead(){


			$this->Lead->add( $_REQUEST['survey'], CPALEAD, $_REQUEST['subid'], $_REQUEST['earn'] * $this->Network->getBanner_ratio(CPALEAD) );
	}	
}
?>